<template>
  <a-modal
    class="noborder"
    :title="title"
    :width="520"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      
      <a-form :form="form">
      <a-form-item label="姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
        <a-input
          v-decorator="[`name`, { initialValue: '', rules: [{ required: true, message: '请输入姓名' }] }]"
        />
      </a-form-item>
        <a-form-item label="手机号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input
            v-decorator="[`mobile`, { initialValue: '', rules: [{ required: true, message: '请输入手机号' }] }]"
          />
        </a-form-item>
        <a-form-item label="密码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input
            v-decorator="[`pwd`, { initialValue: '', rules: [{ required: true, message: '请输入密码,默认123456' }] }]"
          />
           <span class="c-p">默认密码为123456</span>
        </a-form-item>
        
        <a-form-item label="邀请人" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
			showSearch
            placeholder="请选择邀请人"
            v-decorator="['dealer_id']"
			:options="dealerList.type"
			label-in-value
			option-filter-prop="label"
          >
            <!-- <a-select-option :key="null" :value="0" :label="null">无邀请人</a-select-option> -->
            <a-select-option
              v-for="(item, index) in dealerList"
              :key="index"
              :value="item.user_id"
			  :label="item.real_name"
            >{{item.user_id}}-{{ item.real_name }}</a-select-option>
          </a-select>
        </a-form-item>
        
        <a-form-item label="经销商等级" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            placeholder="请选择经销商等级"
            v-decorator="['selectedValueid']"
          >
            <a-select-option :value="0">无等级</a-select-option>
            <a-select-option
              v-for="(item, index) in gradeList"
              :key="index"
              :value="item.grade_id"
            >{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import * as Api from '@/api/user'
import { axios } from '@/utils/request'
export default {
  components: {
  },
  props: {
    // 邀请人uid和姓名
    dealerList:{
        type: Array,
        required: true
    },
    // 经销商等级列表
    gradeList: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      // 对话框标题
      title: '添加经销商',
      // 标签布局属性
      labelCol: { span: 7 },
      // 输入框布局属性
      wrapperCol: { span: 13 },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 当前记录
      record: {},
    }
  },
  methods: {

    // 显示对话框
    handle (record) {
      // 显示窗口
      this.visible = true
      // 当前记录
      this.record = record
      // 设置默认值
      this.setFieldsValue()
    },
    /**
     * 设置默认值
     */
    setFieldsValue () {
      const { record, form: { setFieldsValue } } = this
      this.$nextTick(() => {
        setFieldsValue(pick(record, ['grade_id']))
      })
    },
    /**
     * 确认按钮
     */
    handleSubmit (e) {
      e.preventDefault()
      // 表单验证
      const { form: { validateFields } } = this
      validateFields((errors, values) => {
        // 提交到后端api
        !errors && this.onFormSubmit(values)
      })
    },

    /**
     * 关闭对话框事件
     */
    handleCancel () {
      this.visible = false
      this.form.resetFields()
    },

    /**
    * 提交到后端api
    */
    onFormSubmit (values) {
      // this.confirmLoading = true
      let app = this;
      
      axios({
          url: 'https://ceshi.chenguangjinpu.cn/index.php?s=/api/index/zhucesht', // 你的API接口地址
          method: 'POST',
          data: {
              form: values, // 你要发送的POST数据
          }
      }).then((result) => {
        
        if(result.data.message!='添加成功')
        {
        	if(result.data.message=='手机号已存在')
        	{
        		app.$message.error(result.data.message, 1.5)
        		
        	}else{
            
            app.$message.error(result.data.message, 1.5)
          }
          return;
        }else{
          // 添加后的操作
          axios.post('/user/audit_check', {
             // 这里是你的POST数据  
             user_id: result.data.data, 
             remark: '', 
             is_check: 1
           })  
           .then(result1 => {  
             // 请求成功  
             // console.log(response.data);  
             // 显示成功
             // this.$message.success(result.message, 1.5)
             // // 关闭对话框事件
             // this.handleCancel()
             // // 通知父端组件提交完成了
             // this.$emit('handleSubmit', values)
           })
          app.$message.success(result.data.message, 1.5)
          
          
          // // 关闭对话框事件
          app.handleCancel()
          // // 通知父端组件提交完成了
          app.$emit('handleSubmit', values)
        }
        
          // // 关闭对话框事件
          this.handleCancel()
          // // 通知父端组件提交完成了
          this.$emit('handleSubmit', values)
        })
        .finally(() => {
          this.confirmLoading = false
        })
      
     
    }

  }
}
</script>
<style lang="less">
.ant-form-item {
  margin-bottom: 15px;
}
</style>
